const hasPermission = (code) => {
	let hasPerm = true;
	if (code) {
		hasPerm = filterGlobalPermission(code)
	}
	return hasPerm;
}

/**
 * 全局权限控制
 * 20230803 和张国栋确认不区分type为1或2，只要接口返回了的就有操作权限
 */
export function filterGlobalPermission(code) {
	let hasPerm = true;
	let allPermissionList = [];
	// let authList = JSON.parse(uni.getStorageSync('LOGIN_USER_BUTTON_AUTH') || "[]");
	// let allAuthList = JSON.parse(uni.getStorageSync('SYS_BUTTON_AUTH') || "[]");
	allPermissionList = JSON.parse(uni.getStorageSync('setButtonAuth') || "[]");
	let permissions = allPermissionList.map((item) => item.action)
	if (!permissions.includes(code)) {
		hasPerm = false;
	}
	return hasPerm;
}

/**
 * 全局权限控制
 */
export function filterGlobalPermission_never(code) {
	let hasPerm = true;
	let permissionList = [];
	let allPermissionList = [];

	let authList = JSON.parse(uni.getStorageSync('LOGIN_USER_BUTTON_AUTH') || "[]");
	for (let auth of authList) {
		if (auth.type == '2') {
			permissionList.push(auth);
		}
	}
	let allAuthList = JSON.parse(uni.getStorageSync('SYS_BUTTON_AUTH') || "[]");
	for (let gauth of allAuthList) {
		if (gauth.type == '2') {
			allPermissionList.push(gauth);
		}
	}
	//设置全局配置是否有命中
	let gFlag = true; //禁用命中
	let invalidFlag = false; //无效命中
	if (allPermissionList != null && allPermissionList != "" && allPermissionList != undefined && allPermissionList
		.length > 0) {
		for (let itemG of allPermissionList) {
			if (code === itemG.action) {
				if (itemG.status == '0') {
					invalidFlag = true;
					break;
				} else {
					gFlag = false;
					break;
				}
			}
		}
	}
	if (invalidFlag) {
		return;
	}
	if (permissionList === null || permissionList === "" || permissionList === undefined || permissionList.length <=
		0) {
		hasPerm = gFlag;
	}
	let permissions = [];
	for (let item of permissionList) {
		if (item.type == '2') {
			permissions.push(item.action);
		}
	}
	if (!permissions.includes(code)) {
		hasPerm = false;
	} else {
		for (let item2 of permissionList) {
			if (code === item2.action) {
				gFlag = true;
			}
		}
		hasPerm = gFlag;
	}
	return hasPerm;
}

export default hasPermission;